from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
from root import application as app
import logging

class htmlbuilder:
		
	back_btn_src = "../static/images/back_button.png" 
	def print_header(self):
		header = """
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

		 "http://www.w3.org/TR/html4/loose.dtd"> 
	
		<html>


    <link rel="stylesheet" type="text/css" href="stylesheets/resultlist.css" />
		<head>
 

		</head>"""
		return header
	
	def print_footer(self):
		
		footer = """</body>

		</html>"""
		return footer
	

	def get_html_code(self, root_dir, src):
		
		path = os.path.join(root_dir, src)
		f = open(path, 'r')
		html = f.read()
		f.close()
		return html

	def print_body(self,day,events):
  
		root_dir = os.path.dirname(app.__file__)
		back_button = self.get_html_code(root_dir, 'html/back_button.html')
		home_button = self.get_html_code(root_dir, 'html/home_button.html')		
		refresh_button = self.get_html_code(root_dir, 'html/refresh_button.html')

		body = ""
		body_title = """<body> <p class = "side_title"> <font size = "30">"""+ self.to_full_day(day) +"""</font> """
		
		title_close = """</p>"""
		
		body+=body_title + back_button + refresh_button + home_button + title_close
		body+="""<div class = day_items align=center>"""
		
		body+= self.print_multiple_containers(events)
		
		body+="""</div>"""
		body += "</body>"
		 
		return body

	def print_venue_body(self, events):
		root_dir = os.path.dirname(app.__file__)
		back_button = self.get_html_code(root_dir, 'html/back_button.html')
		home_button = self.get_html_code(root_dir, 'html/home_button.html')		
		refresh_button = self.get_html_code(root_dir, 'html/refresh_button.html')

		body = ""
		body_title = """<body> <p class = "side_title"> <font size = "30"> Venues </font> """
		
		title_close = """</p>"""
		
		body+=body_title + back_button + refresh_button + home_button + title_close
		body+="""<div class = day_items align=center>"""
		
		body+= self.print_venue_containers(events)
		
		body+="""</div>"""
		body += "</body>"
		 
		return body

	def print_multiple_containers(self,containers):
		containers_result=""
		for event in containers:
			containers_result += self.print_container(event)
		return containers_result
	
	def print_venue_containers(self, venues):
		return '\n'.join([self.print_venue_container(venue) for venue in venues])
		
		
	def print_icons(self, icons):
		icon_container ="""<div class = "entry_details">"""
		
		for icon in icons:
			icon_list ="""<img class = "zoom" src="images/"""
			icon += ".png" +'"'
			icon +=""" alt="">"""
			icon_list+=icon
			icon_container+=icon_list
			
		icon_container+="""</div>"""
		return icon_container
	
	
	'''
	Instead of day as an argument, why not use  key-value pair
	and make this function generic for days, genres etc etc?
	'''
	def generate(self,events, day):
		finalstring=""
		finalstring += self.print_header()
		finalstring += self.print_body(day,events)
		finalstring += self.print_footer()
		return finalstring;

	def gen_venues(self, venues):
		finalstring = ''
		finalstring += self.print_header()
		finalstring += self.print_venue_body(venues)
		finalstring += self.print_footer()
		return finalstring

	def create_club_icon(self, url, icon):
		if url =="":
			return ""
		if icon =="":
			return ""
		
		result=""
		result += "<a href=" +'"' + url + '"' + " target="+'"'+"_blank" +'"' +'>'
		
		result += """<img class = "profilezoom" src=""" + "" +'"'+ icon +'"' +" alt"+'>'
		result += "</a>"
			
		return result
	
	def print_container(self, event):
		container=""
		container+="""<div class = "container">"""
		
		container +="<div class=event_title>"
		container += self.create_club_icon(event['link'], event['profile_link'])
		container += self.print_title(event['name'], event['location']) 
		container +="</div>"
		container += self.print_age_limit(event['age'])
		container += self.print_vip(event['VIP'])
    
    # TODO: change background colour of promoted events.  
    # Google Search style
		#container += self.print_promoted(event['promoted'])
		container += self.print_start_time(event['start_time'])
		container += self.print_location(event['location'])
		#container += self.print_RSVP(event['id'])
    # container += self.print_more_information();
	   
		container+="""</div>"""
		return container
	
	def print_venue_container(self, venue):
		container = '<div class = "container"> <div class=event_title>'
		container += self.create_club_icon("http://www.facebook.com/%d"% venue["clubID"], venue["clubPicURL"])
		container += self.print_title(venue["clubName"])
		container += "</div>"
		container += "\n<p class = event_title> Location: " + venue['clubAddr'] + "</p>"
		container += "\n<p class = event_title> Email: " + venue['clubMail'] + '</p>'
		container += "\n<p class = event_title> Phone: " + str(venue['clubNo']) + '</p>'
		#container += self.create_club_icon("www.facebook.com/%d"% venue["clubID"], 'http://i.imgur.com/w0cZb.gif')
		
		container += """</div>"""
		return container

	def print_title(self, name, location="blank"):
			result= "<b class='event_title'>" + name + "</b><br/>"	
			return result
   
	def print_more_information(self):
		result="<a href=" +'"' + "\EventDetails" + '"' + '>'
		result += """<img class = "info" src=""" +'"'+"images/MoreInformation.png" +'"' +" alt" +'/>'
		result += "</a>"
		return result
	
	def print_club_icon(self,url):
		
		if url =="":
			return ""
		
		result ="""<img class = "zoom" src="images/"""
		result += url +".png" +'"'
		result +=""" alt=""/>"""
		return result;
	
	def print_event_name(self,name):
		
		if name =="":
			return ""
		
		result ="""<img class = "zoom" src="images/"""
		result += name +".png" +'"'
		result +=""" alt=""/>"""
		return result;
		
		return result;
	
	def print_age_limit(self, age):
		
		if age =="":
			return ""
		
		result ="""<img class = "zoom" src="images/"""
		result += age +".png" +'"'
		result +=""" alt=""/>"""
		return result;
	
	def print_vip(self,vip):
		if vip =="":
			vip = "VIP_GREY"
		
		result ="""<img class = "zoom" src="images/"""
		result += vip +".png" +'"'
		result +=""" alt=""/>"""
		return result;
		
	
	def print_promoted(self, promoted):
		if promoted =="":
			promoted = "Promoted_GREY"
		
		result ="""<img class = "zoom" src="images/"""
		result += promoted +".png" +'"'
		result +=""" alt=""/>"""
		return result;
	
	def print_start_time(self, start_time):
		
		if start_time =="":
			return ""
	   
		start_time = str(int(start_time[0:2]) -12 ) 
		result ="""<img class = "zoom" src="images/"""
		result += start_time +".png" +'"'
		result +=""" alt=""/>"""
		return result;
	
	def print_location(self,url):
		if url =="":
			return ""
		
		result =""
		url = self.put_into_gmaps_url(url)
		result += "<a href=" +'"' +url+'"' + " target=" +'"' +"_blank" +'"' +'>'
		
		result += """<img class = "zoom" src=""" +'"' +"images/Location.png" +'"' +'/>'
		result += "</a>"
			
		return result
    
			
	def put_into_gmaps_url(self,addr):
		encoded = ""
		
		for c in addr:
			
			if c == ' ':
				c = '+'
				
			encoded += c
		url = "https://maps.google.ie/maps?q=" + encoded
		return url 
	
	
	def to_full_day(self,day):
		days = {}
	   	days['Mon'] = "Monday"	
	   	days['Tue'] = "Tuesday"	
	   	days['Wed'] = "Wednesday"	
	   	days['Thu'] = "Thursday"	
	   	days['Fri'] = "Friday"	
	   	days['Sat'] = "Saturday"	
	   	days['Sun'] = "Sunday"	
		try:
			return days[day]
		except:
			return day
		
    
	def print_RSVP(self, event_id):
		
		result = ''
		access_token = ''
		# PUT IN TO UTILS
		try:
			token_path = os.path.join(os.path.dirname(app.__file__), 'token') 
			f = open(token_path, 'r')
			access_token = 'access_token=' + f.read() 
			f.close()
		except:
			logging.error("Could not retrieve access token.")
			
		event_id = '"'+str(event_id)+'"'
		access_token = '"' + access_token + '"'
		result = '<div class = "rsvp_form"> <form action="" method="post"><input type="radio" name="rsvp" value="attending"/>Attending</br><input type="radio" name="rsvp" value="maybe"/>Maybe</br><input type="radio" name="rsvp" value="declined"/>Not Attending  <input type="submit" value="RSVP" /></form></div>'
		return result